#!/usr/bin/env python
# encoding: utf-8
"""
sampleapi.py

This application will list a folder you specify showing the steps you would
take to perform a series of API actions.

Created by Barry Ruffner on 2010-10-14.
Copyright (c) 2008-2010 Nirvanix. All rights reserved.
"""

import sys
import os
import os.path
from nirvanix import Nirvanix
from nirvanix import Session
import nirvanixutils

def listfolder(session):
	folderpath = raw_input("Path to list: ")
	imfs = Nirvanix('IMFS', session)
	options = {'folderPath':folderpath,'pageNumber':'1','pageSize':'500'}
	try:
		doc = imfs.ListFolder(options, False)
	except nirvanixutils.NirvanixException, e:
		print e
		print e.code
		return
	folders = doc.getElementsByTagName("Folder")
	print "\nFolders:\n\n"
	for foldernode in folders:
		print foldernode.getElementsByTagName("Name")[0].childNodes[0].data

	files = doc.getElementsByTagName("File")
	print "\nFiles:\n\n"
	for filenode in files:
		print filenode.getElementsByTagName("Name")[0].childNodes[0].data

def copyfile(session):
	sourcepath = raw_input("Source File Path: ")
	destpath = raw_input("Destination Folder Path: ")
	imfs = Nirvanix('IMFS', session)
	options = {'srcFilePath':sourcepath,'destFolderPath':destpath}
	try:
		imfs.CopyFiles(options, False)
	except nirvanixutils.NirvanixException, e:
		print e
		print e.code
		return
	print "Copy Complete."
	
def movefile(session):
	sourcepath = raw_input("Source File Path: ")
	destpath = raw_input("Destination Folder Path: ")
	imfs = Nirvanix('IMFS', session)
	options = {'srcFilePath':sourcepath,'destFolderPath':destpath}
	try:
		imfs.MoveFiles(options, False)
	except nirvanixutils.NirvanixException, e:
		print e
		print e.code
		return
	print "Move Complete."

def deletefile(session):
	sourcepath = raw_input("File Path To Delete: ")
	if raw_input("Are you sure (y / n)? ") != 'y':
		print "Delete cancelled."
		return
	imfs = Nirvanix('IMFS', session)
	options = {'filepath':sourcepath}
	try:
		imfs.DeleteFiles(options, False)
	except nirvanixutils.NirvanixException, e:
		print e
		print e.code
		return
	print "Delete Complete."
		
def main():
	args = sys.argv[1:]
	if len(args) < 3:
		print "\r\nUsage: AppKey Username Password"
		print "Example: python sampledownloader.py XXXX-XXXX-XXXX MyUser MyPassword\r\n"
		return
	# assign args to nice names
	appKey = args[0]
	username = args[1]
	password = args[2]
	
	# Clear the screen.
	print '\x1b[H\x1b[2J'
	
	# Create a session object
	try:
		session = Session(appKey, username, password)
		while 1==1:
			print "1. listfolder\n"
			print "2. copyfile\n"
			print "3. movefile\n"
			print "4. deletefile\n"
			print "0. exit\n"
			action = raw_input("Please select a menu item: ")
			if (action == '1'):
				listfolder(session)
			elif (action == '2'): 
				copyfile(session)
			elif (action == '3'): 
				movefile(session) 
			elif (action == '4'): 
				deletefile(session)
			else:
				break
	except nirvanixutils.NirvanixException, e:
		if e.code == 80001:
			print "Login Failed, Please correct the username and password you provided and try again."
			return
		else:
			print "The following error occured while logging in: " + e.message
	
if __name__ == '__main__':
	main()